package org.miku.junit;

import static org.junit.Assert.assertEquals;

import java.util.Vector;

import org.junit.Test;
import org.miku.db.SQLHelper;
import org.miku.db.TableQuery;
import org.miku.utils.Condition;
import org.miku.utils.ContentValue;

public class TableQueryTest {
	SQLHelper sqlHelper = new SQLHelper("localhost",
			3306, "db_onlinenote", "root", "aihejuhuacha");
	TableQuery tbQuery = new TableQuery("user", sqlHelper);
	
	/* @Test
	public void testParse() {
		String result = tbQuery.parse(new String[]{"hello","hi"}, false);
		
		assertEquals("hello,hi", result);
	}
	
	 @Test
	public void testExtraPair() {
		ContentValue cv = new ContentValue();
		cv.addPair("first name", "John");
		cv.addPair("last name", "Steve");
		Vector<String[]> result = tbQuery.extractPair(cv);

		assertEquals(2, result.get(0).length);
		assertEquals(2, result.get(1).length);
	}
	
	@Test
	public void testWrapperPair() {
		ContentValue cv = new ContentValue();
		cv.addPair("username", "paintor");
		cv.addPair("email", "soxfmr@foxmail.com");
		cv.addPair("age", 20);
		String result = tbQuery.internal_wrapperPair(cv, 
				new String[]{"and", "or"}, null);
		
		System.out.println(result);
	}
	
	@Test
	public void testRecordCount() {
		// int count = tbQuery.recordCount();
		// int count = tbQuery.recordCount("id", "2");
		ContentValue cv = new ContentValue();
		cv.addPair("id", 2);
		cv.addPair("id", 3);
		Condition cd = new Condition(cv, new String[]{"or"});
		int count = tbQuery.recordCount(cd);
		assertEquals(2, count);
	}

	@Test
	public void testQuery() {
		ContentValue cv = new ContentValue();
		cv.addPair("id", 1);
		cv.addPair("id", 2);
		Condition cd = new Condition(cv, new String[]{"or"});
		ResultSet result = tbQuery.query(new String[]{"username"}, cd);
		
		try {
			result.first();
			assertEquals("John", result.getString(1));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testQuerySingle() {
		String username = tbQuery.querySingle("username", "id", "1");

		assertEquals("John", username);
	}
	
	@Test
	public void testAdd() {
		ContentValue cv = new ContentValue();
		cv.addPair("nickname", "Andy");
		cv.addPair("secQuestion", "Hi");
		cv.addPair("secAnswer", "Hello");
		cv.addPair("registerTime", "2015");
		cv.addPair("username", "Andy");
		cv.addPair("password", "password");
		tbQuery.add(cv);
		
		String nickname = tbQuery.querySingle("nickname", "id", "13");

		assertEquals("Andy", nickname);
	}
	
	@Test
	public void testAdd() {
		ContentValue cv = new ContentValue();
		cv.addPair("username", "Mason");
		tbQuery.update(cv, "username", "Andy");

		String nickname = tbQuery.querySingle("username", "id", "13");
		
		assertEquals("Mason", nickname);
	}
	
	@Test
	public void testAdd() {
		ContentValue cv = new ContentValue();
		cv.addPair("id", "3");
		cv.addPair("username", "John");

		tbQuery.remove(new Condition(cv, new String[]{"and"}));
	}*/
}
